<?php


namespace app\api\controller\v1;


use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use think\Config;

/**
 * 文件接口
 */
class File extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * 上传文件
     * @ApiMethod (POST)
     * @param File $file 文件流
     */
    public function upload()
    {
        if($this->request->isPost()){

            Config::set('default_return_type', 'json');
            $chunkid = $this->request->post('chunkid');
            if ($chunkid) {
                if (!Config::get('upload.chunking')) {
                    $this->error(__('Chunk file disabled'));
                }

                $action = $this->request->post('action');
                $chunkindex = $this->request->post('chunkindex/d');
                $chunkcount = $this->request->post('chunkcount/d');
                $filename = $this->request->post('filename');
                $method = $this->request->method(true);

                if ($action == 'merge') {
                    $attachment = null;
                    //合并分片文件
                    try {
                        $upload = new Upload();
                        $attachment = $upload->merge($chunkid, $chunkcount, $filename);
                    } catch (UploadException $e) {
                        $this->error($e->getMessage());
                    }
                    $this->success(__('Uploaded successful'), ['url' => $attachment->url]);
                } elseif ($method == 'clean') {
                    //删除冗余的分片文件
                    try {
                        $upload = new Upload();
                        $upload->clean($chunkid);
                    } catch (UploadException $e) {
                        $this->error($e->getMessage());
                    }

                    $this->success();
                } else {
                    //上传分片文件
                    //默认普通上传文件
                    $file = $this->request->file('file');
                    try {
                        $upload = new Upload($file);
                        $upload->chunk($chunkid, $chunkindex, $chunkcount);
                    } catch (UploadException $e) {
                        $this->error($e->getMessage());
                    }
                    $this->success();
                }
            } else {
                $attachment = null;
                //默认普通上传文件
                $file = $this->request->file('file');
                try {
                    $upload = new Upload($file);
                    $attachment = $upload->upload();
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }

                $this->success(__('Uploaded successful'), ['url' => $attachment->url]);
            }
        }else{
            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
        }
    }

    public function uploadServer($param)
    {
        $attachment = null;
        //默认普通上传文件
        $file = $this->request->file($param);
        try {
            $upload = new Upload($file);
            $attachment = $upload->upload();
        } catch (UploadException $e) {
            $this->error($e->getMessage());
        }
        return $attachment->url;
    }
}